package cn.tp.ksoft.civil_driver.module.order;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.Date;

import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.OrderBean;
import cn.tp.ksoft.civil_driver.bean.OrderDetailBean;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.request.DriverOrderRequest;
import cn.tp.ksoft.civil_driver.utils.Constants;
import cn.tp.ksoft.civil_driver.utils.TimestampTypeAdapter;
import cn.tp.ksoft.civil_driver.utils.Utils;
import cn.tp.ksoft.library_civil.utils.DateUtil;

/**
 * Created by zhusong on 16/5/2.
 */
public class OrderDetailActivity extends DriverBaseActivity implements View.OnClickListener {

    private final int REQUEST_REFUSE = 1;

    private OrderBean orderBean;
    private OrderDetailBean detailBean;
    private Gson gson;
    private TextView mAddressTv;
    private TextView mUsrInfoTv;
    private TextView mTimeTv;
    private TextView mPriceTv;
    private TextView mDurationTv;
    private TextView mDurationUnitTv;

    private DriverOrderRequest request;

    private final int DAY_HOURS = 8;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_activity_receipt_order_detail);
        updateTitle(getString(R.string.driver_receipt_order));
        Intent intent = getIntent();
        if (null != intent) {
            orderBean = (OrderBean) intent.getSerializableExtra(Constants.KEY_ORDER_BEAN);
            if (null != orderBean) {
                GsonBuilder gsonBuilder = new GsonBuilder();
                gsonBuilder.registerTypeAdapter(Date.class, new TimestampTypeAdapter());
                gson = gsonBuilder.create();
                detailBean = gson.fromJson(orderBean.getDetail(),
                        OrderDetailBean.class);
            }
        }
        mAddressTv = (TextView) findViewById(R.id.tv_address);
        mUsrInfoTv = (TextView) findViewById(R.id.tv_usr_info);
        mTimeTv = (TextView) findViewById(R.id.tv_time);
        mPriceTv = (TextView) findViewById(R.id.tv_price);
        mDurationTv = (TextView) findViewById(R.id.tv_duration);
        mDurationUnitTv = (TextView) findViewById(R.id.tv_duration_unit);
        findViewById(R.id.btn_phone).setOnClickListener(this);
        findViewById(R.id.btn_refuse).setOnClickListener(this);
        findViewById(R.id.btn_receipt).setOnClickListener(this);

        if (null != detailBean) {
            mAddressTv.setText(detailBean.getStartAddress());
            mUsrInfoTv.setText(orderBean.getName() + "    " + orderBean.getTel());
            mTimeTv.setText(DateUtil.D2Syrsf(detailBean.getBycarStartTime()) + "出发");
            mPriceTv.setText(String.valueOf(orderBean.getOrderMoney()));
            int day = detailBean.getBycarLong() / DAY_HOURS;
            if (day > 0) {
                mDurationTv.setText(String.valueOf(day));
                mDurationUnitTv.setText(getString(R.string.driver_unit_day));
            } else {
                mDurationTv.setText(String.valueOf(detailBean.getBycarLong()));
                mDurationUnitTv.setText(getString(R.string.driver_unit_hour));
            }
        }

        request = new DriverOrderRequest(this, new WeakHandler(this));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_REFUSE) {
            if (resultCode == RESULT_OK) {
                setResult(RESULT_OK);
                finish();
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_phone: {
                if (!Utils.isPhone(orderBean.getTel())) {
                    Toast.makeText(this, "未识别的手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                //用intent启动拨打电话
                try {
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+orderBean.getTel()));
                    startActivity(intent);
                } catch (SecurityException e) {
                    Toast.makeText(this, "拨号功能被禁用", Toast.LENGTH_SHORT).show();
                }

                break;
            }
            case R.id.btn_refuse: {
                Intent intent = new Intent(this, RefuseOrderActivity.class);
                intent.putExtra(Constants.KEY_ORDER_ID, orderBean.getId());
                startActivityForResult(intent, REQUEST_REFUSE);
                break;
            }
            case R.id.btn_receipt: {
                request.confirmOrder(orderBean.getId());
                break;
            }
        }
    }

    @Override
    protected void handleFailureResult(int what, @NonNull ResultBean result) {
        super.handleFailureResult(what, result);
        if (what == REQUEST_ORDER_COFIRM) {
            finish();
        }
    }

    @Override
    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
        switch (what) {
            case REQUEST_ORDER_COFIRM : {
                setResult(RESULT_OK);
                finish();
                break;
            }
        }
    }
}
